package com.pain.scaffold.common.utils;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.lang.reflect.Field;  
import java.util.ArrayList;  
import java.util.List;  
  
public class ExcelGenerator {  
  
    public static void main(String[] args) throws IOException, IllegalAccessException {  
        List<MyData> dataList = new ArrayList<>();  
        dataList.add(new MyData("John", 30));  
        dataList.add(new MyData("Jane", 25));  
        dataList.add(new MyData("Doe", 40));  
  
        String excelFilePath = "D://output.xlsx";
        generateExcel(dataList, excelFilePath);  
    }  
  
    public static void generateExcel(List<?> dataList, String excelFilePath) throws IOException, IllegalAccessException {  
        Workbook workbook = new XSSFWorkbook(); // 创建一个新的Excel工作簿  
        Sheet sheet = workbook.createSheet("Sheet1"); // 创建一个新的工作表  
  
        if (dataList != null && !dataList.isEmpty()) {  
            // 获取第一个对象的类信息  
            Class<?> clazz = dataList.get(0).getClass();  
              
            // 获取字段列表  
            Field[] fields = clazz.getDeclaredFields();  
              
            // 创建表头行  
            Row headerRow = sheet.createRow(0);  
            for (int i = 0; i < fields.length; i++) {  
                fields[i].setAccessible(true); // 允许访问私有字段  
                Cell headerCell = headerRow.createCell(i);  
                headerCell.setCellValue(fields[i].getName()); // 设置表头为字段名  
            }  
              
            // 创建数据行  
            for (int rowNum = 1; rowNum <= dataList.size(); rowNum++) {  
                Row row = sheet.createRow(rowNum);  
                Object data = dataList.get(rowNum - 1);  
                for (int colNum = 0; colNum < fields.length; colNum++) {  
                    Cell cell = row.createCell(colNum);  
                    Object fieldValue = fields[colNum].get(data);  
                    cell.setCellValue(fieldValue != null ? fieldValue.toString() : "");  
                }  
            }

            // 自动调整列宽
            for (int i = 0; i < fields.length; i++) {
                sheet.autoSizeColumn(i);
            }
        }  
  

  
        // 将工作簿写入文件  
        try (FileOutputStream outputStream = new FileOutputStream(excelFilePath)) {  
            workbook.write(outputStream);  
        }  
  
        // 关闭工作簿  
        workbook.close();  
    }  
  
    // 假设我们有一个简单的类，包含姓名和年龄两个字段  
    public static class MyData {  
        private String name;  
        private int age;  
  
        public MyData(String name, int age) {  
            this.name = name;  
            this.age = age;  
        }  
  
        // getters and setters ...  
  
        public String getName() {  
            return name;  
        }  
  
        public void setName(String name) {  
            this.name = name;  
        }  
  
        public int getAge() {  
            return age;  
        }  
  
        public void setAge(int age) {  
            this.age = age;  
        }  
    }  
}